package top.laoliu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.laoliu.pojo.PositionResume;
import top.laoliu.pojo.PositionVo;
import top.laoliu.pojo.User;
import top.laoliu.service.DeliveryService;
import top.laoliu.service.PositionService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: acer
 * @Date: 2020/10/13 18:13
 * @Description:
 */
@Controller
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;

    @Autowired
    private DeliveryService deliveryService;

    @RequestMapping("/positionDetails/{position_id}")
    public String toPositionDetails(@PathVariable Integer position_id, Model model, HttpSession session){
        User user = (User)session.getAttribute("user");
        if (user!=null){
            Integer user_id = user.getUser_id();
            Integer deliveryStatus = deliveryService.getDeliveryStatus(new PositionResume(position_id, user_id, null, null));
            model.addAttribute("deliveryStatus",deliveryStatus);
        }
        PositionVo position = positionService.getPositionById(position_id);
        position.setBrowsing_number(position.getBrowsing_number()+1);
        System.out.println(position);
        positionService.updatePosition(position);
        model.addAttribute("position",position);
        List<PositionVo> otherPositions = positionService.getLimitedOtherPositions(position.getIndustry(), position.getPosition_id(), 0, 3);
        model.addAttribute("otherPositions",otherPositions);
        System.out.println(otherPositions);
        return "positionDetails";
    }

    @RequestMapping("/searchPosition")
    public String toSearchPosition(String position,String workplace,String classification,Model model){
        List<PositionVo> positions = positionService.getPositionsBySearchTime(position, workplace,classification, 1, 7);
        Integer totalCount = positionService.getPositionTotalCountBySearch(position, workplace,classification);
        List<String> positionNames = positionService.getLimitedPositionName();
        model.addAttribute("positionNames",positionNames);
        model.addAttribute("positions",positions);
        model.addAttribute("positionTotalCount",totalCount);
        model.addAttribute("position",position);
        model.addAttribute("workplace",workplace);
        model.addAttribute("pageSize",positions.size());
        return "searchPosition";
    }
    @RequestMapping("/getPositionSortedByBrowse")
    public String getPositionSortedByBrowse(String position,String workplace,String classification,Model model){
        List<PositionVo> positions = positionService.getPositionsBySearchBrowsing(position, workplace, 0, 7);
        Integer totalCount = positionService.getPositionTotalCountBySearch(position, workplace,classification);
        model.addAttribute("positions",positions);
        model.addAttribute("positionTotalCount",totalCount);
        model.addAttribute("pageSize",positions.size());
        return "searchPosition";
    }

    @RequestMapping("/getPagePosition")
    @ResponseBody
    public List<PositionVo> getPagePosition(Integer currentPage,String position,String classification,String workplace){
        List<PositionVo> positions = positionService.getPositionsBySearchTime(position, workplace, classification,currentPage, 7);
        return positions;
    }
}
